package com.zyyoona7.wheelpicker.slice;

import com.zyyoona7.picker.DatePickerViewEx;
import com.zyyoona7.picker.util.SelectButton;
import com.zyyoona7.wheelpicker.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 自定义DatePicker样例
 *
 * @author gc
 * @since 2021-04-13
 */
public class MainAbility3Slice extends AbilitySlice {
    private TextField mYear;

    private TextField mMonth;

    private TextField mDay;

    private Checkbox mCheckBox;

    private DatePickerViewEx datePickerViewEx;

    private static final int num20 = 20;
    private static final int num6 = 6;
    private static final int num2 = 2;
    private static final int num8 = 8;
    private static final int num12 = 12;

    /**
     * onStart
     *
     * @param intent
     */
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main3);

        mYear = (TextField) findComponentById(ResourceTable.Id_year_input);
        mMonth = (TextField) findComponentById(ResourceTable.Id_month_input);
        mDay = (TextField) findComponentById(ResourceTable.Id_day_input);
        mCheckBox = (Checkbox) findComponentById(ResourceTable.Id_check_box);
        mCheckBox.addDrawTask((component, canvas) -> drawCheckBox(component, canvas));

        mCheckBox.setTouchEventListener((component, touchEvent) -> {
            component.invalidate();
            return true;
        });

        SelectButton mSetYear = (SelectButton) findComponentById(ResourceTable.Id_set_year);
        datePickerViewEx = (DatePickerViewEx) findComponentById(ResourceTable.Id_operator_date_picker);

        mSetYear.setClickedListener(component -> {
            if (mYear.getText() != null && !mYear.getText().equals("")) {
                if (isNum(mYear.getText())) {
                    int year = Integer.parseInt(mYear.getText());
                    datePickerViewEx.setYear(year);
                } else {
                    showDialog("非法输入!");
                }
            }
        });

        SelectButton mSetMonth = (SelectButton) findComponentById(ResourceTable.Id_set_month);

        mSetMonth.setClickedListener(component -> {
            if (mMonth.getText() != null && !mMonth.getText().equals("")) {
                if (isNum(mMonth.getText())) {
                    int month = Integer.parseInt(mMonth.getText());
                    datePickerViewEx.setMonth(month);
                } else {
                    showDialog("非法输入!");
                }
            }
        });

        SelectButton mSetDay = (SelectButton) findComponentById(ResourceTable.Id_set_day);
        mSetDay.setClickedListener(component -> {
            if (mDay.getText() != null && !mDay.getText().equals("")) {
                if (isNum(mDay.getText())) {
                    int day = Integer.parseInt(mDay.getText());
                    datePickerViewEx.setDay(day);
                } else {
                    showDialog("非法输入!");
                }
            }
        });
    }

    private void drawCheckBox(Component component, Canvas canvas) {
        final double height = component.getHeight();
        Paint paint = new Paint();
        paint.setColor(new Color(Color.getIntColor("#FFE337BC")));
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setStrokeWidth(num6);
        canvas.drawCircle( (float) height / num2 - num8, (float)height / num2, num20, paint);
        if (((Checkbox) component).isChecked()) {
            Paint paint2 = new Paint();
            paint2.setColor(new Color(Color.getIntColor("#FFE337BC")));
            paint2.setStyle(Paint.Style.FILL_STYLE);
            canvas.drawCircle((float)(height / num2 - num8), (float) (height / num2), num12, paint2);
        }
    }

    private void showDialog(String info) {
        ToastDialog toastDialog = new ToastDialog(getContext());
        toastDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setText(info);
        Text toastText = (Text) toastDialog.getComponent();
        if (toastText != null) {
            toastText.setMultipleLine(true);
            toastText.setTextSize(num20, Text.TextSizeType.FP);
            toastText.setPaddingBottom(num20);
            toastText.setPaddingTop(num20);
        }
        toastDialog.show();
    }

    /**
     * 是否为数字
     *
     * @param num 字符串
     * @return 是否
     */
    public boolean isNum(String num) {
        boolean isNums = false;
        Pattern pattern = Pattern.compile("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
        Matcher isNum = pattern.matcher(num);
        if (isNum.matches()) {
            isNums = true;
        }
        return isNums;
    }

    /**
     * onActive
     */
    @Override
    protected void onActive() {
        super.onActive();
    }

    /**
     * onForeground
     *
     * @param intent
     */
    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
